from StartGroup.models import Group
from announcement.models import Announcement
from django import forms
from django.conf import settings
from django import forms
import datetime
from django.forms.extras.widgets import SelectDateWidget
from datetime import date
from utils.widgets import DatePicker

attrs_dict = { 'class': 'required' }

class AnnouncementForm(forms.Form):
    
    title = forms.CharField(max_length=50, required=True, label='TITLE', error_messages={'required': 'Please enter Title'},
                                widget=forms.TextInput(attrs={'size':'50', 'id':'id_title'}))
    description = forms.CharField(max_length=300, required=False, label='DESCRIPTION',
                                 widget=forms.Textarea(attrs={'size':'300', 'cols':'100', 'rows':'3', 'id':'id_descritption'}))
    created_date = datetime.date.today()
    
    # Validation: Title is required and must be smaller than 51 characters
    def clean_title(self):
        print "hi"
        if (len(self.cleaned_data['title']) < 1): 
            raise forms.ValidationError((u'Title field is required'))
        elif (len(self.cleaned_data['title']) > 50):
            raise forms.ValidationError((u'Title field must be shorter than 50 characters.'))
        
        return self.cleaned_data['title']
    
    # Validation: Description is required and must be smaller than 301 characters
    def clean_description(self):
        print "descri"
        if (len(self.cleaned_data['description']) > 300):
            print "description"
            raise forms.ValidationError((u'Description field must be shorter than 300 characters.'))
        
        return self.cleaned_data['description'] 
    
    def save(self, user_id, group_info):
        new_announcement = Announcement(group = group_info,
                                title=self.cleaned_data['title'],
                                description=self.cleaned_data['description'],
                                post_by = user_id,
                                )
        new_announcement.save()
        return new_announcement